<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    //用户设置页面
    public function setting()
    {
        return view('user.setting');
    }

    //保存用户设置
    public function settingStore()
    {
        return null;
    }

    //显示用户信息
    public function show(User $user)
    {
        //这个人的信息, 含 关注、粉丝、文章数
        $user = User::withCount(['fans','stars', 'posts'])->find($user->id);

        //这个人的文章列表, 取创建时间最新的 10 条
        $posts = $user->posts()->orderBy('created_at', 'desc')->take(10)->get();

        //这个人关注的用户, 包含关注用户的 关注、粉丝、文章数
        $stars = $user->stars;
        $susers = User::whereIn('id', $stars->pluck('star_id'))->withCount(['fans','stars', 'posts'])->get();

        //这个人的粉丝 关注、粉丝、文章数
        $fans = $user->fans;
        $fusers =  User::whereIn('id', $fans->pluck('fan_id'))->withCount(['fans','stars', 'posts'])->get();

        return view('user/show', compact('user', 'posts', 'susers', 'fusers'));
    }

    //关注某个用户
    public function fan(User $user)
    {
        $me = \Auth::user();
        $res = $me->doFan($user->id);
        dd($res);

        return [
            'error' => 0,
            'msg'   => ''
        ];
    }

    //取消关注某个用户
    public function unfan(User $user)
    {
        $me = \Auth::user();
        $me->doUnfan($user->id);

        return [
            'error' => 0,
            'msg'   => ''
        ];
    }
}
